<template>
  <div class="container">
    <router-view/>
    <el-container>
      <el-header>标签管理</el-header>
      <el-main>
        <el-row>
          <el-col :span="3" class="input-col">
            <el-input v-model="form.name" placeholder="请输入关键字"></el-input>
          </el-col>
          <el-col :span="3" class="input-col">
            <el-button type="primary" @click="findAll" class="aa">查询</el-button>
          </el-col>
          <el-col :span="13" class="input-col" align="right">
            <el-button type="primary" @click="toAdd" class="aa">新增标签</el-button>
          </el-col>
        </el-row>

        <el-table
            :data="tableData"
            border
            @selection-change="handleSelectionChange"
            style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="编号"></el-table-column>
          <el-table-column prop="name" label="标签名称"></el-table-column>
          <el-table-column prop="people" label="添加人"></el-table-column>
          <el-table-column prop="modificationtime" label="最后修改时间"></el-table-column>
          <el-table-column align="center" prop="residentStatus" label="状态" width="150">
            <template slot-scope="scope">
              <el-switch
                  v-model="scope.row.status"
                  active-color="#13ce66"
                  inactive-color=""
                  :active-value="1"
                  :inactive-value="0"
                  @change="handleStatusChange(scope.row)"
              ></el-switch>
              <span v-if="scope.row.status" class="switch-text">启用</span>
              <span v-else class="switch-text2">禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button type="primary" @click="toUpdate(scope.row) "class="aa">编辑</el-button>
              <el-button type="danger" @click="toDelete(scope.row.id)"class="aa">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

<!--        <el-pagination-->
<!--            background-->
<!--            layout="total, prev, pager, next"-->
<!--            :total="total"-->
<!--            @size-change="handleSizeChange"-->
<!--            @current-change="handleCurrentChange"-->
<!--            :current-page="current"-->
<!--            :page-size="pageSize">-->
<!--        </el-pagination>-->

        <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
          <el-form ref="form" :model="pojo" label-width="80px">
            <el-form-item label="标签名称">
              <el-input v-model="pojo.name"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="change()">确 定</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      form: {},
      total: 0,
      current: 1,
      pageSize: 100,
      title: "添加",
      dialogVisible: false,
      pojo: {},
      tx: [],
    };
  },
  methods: {
    handleStatusChange(row){
      this.axios.post(`http://localhost:8230/management/qh`,row).then(res=>{
        if(res.data.code==200){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
        }else{
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }
      })
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.findAll();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.findAll();
    },
    findAll() {
      this.axios.post(`http://localhost:8230/management/aa/${this.current}/${this.pageSize}`, this.form)
          .then(res => {
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          })
          .catch(error => {
            console.error('请求失败:', error);
          });
    },
    toAdd() {
      this.title = "添加";
      this.pojo = {};
      this.dialogVisible = true;
    },
    change() {
      this.axios.post(`http://localhost:8230/management/change`, this.pojo)
          .then(res => {
            if (res.data) {
              this.dialogVisible = false;
              this.findAll();
            }
          })
          .catch(error => {
            console.error('请求失败:', error);
          });
    },
    toUpdate(obj) {
      this.title = "修改";
      this.pojo = {...obj};
      this.dialogVisible = true;
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete(`http://localhost:8230/management/delete/${id}`)
            .then(res => {
              if (res.data) {
                this.findAll();
              }
            })
            .catch(error => {
              console.error('请求失败:', error);
            });
      }).catch(() => {
      });
    },
  },
  created() {
    this.findAll();
  }
}
</script>

<style>
.container {
  background-color: #F2F7FB;
  border: 1px solid #d3dce6;
  border-radius: 5px;
  padding: 10px;
  margin: 10px;
  width: 50%;
  height: 50%;
  overflow: auto;
}

.input-col {
  background-color: #F2F7FB;
}

.aa{
  width: 100px;
  height: 30px;
}
</style>